﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 纺织贸易管理系统;

namespace BLL
{
   public static  class 盘亏出库单BLL
    {

        public static void 保存单据(DanjuTable danju,List <danjumingxitable > danjumingxitables ,List<JuanHaoTable> juanHaoTables)
        {
            if (SysInfo.GetInfo.own != "审核制")
            {
                danju.zhuangtai = "已审核";
            }
            else
            {
                danju.zhuangtai = "未审核";
            }
            danju.dh = 单据BLL.检查单号(danju.dh);
            DanjuTableService.InsertDanjuTable(danju);
            foreach (var m in danjumingxitables.Where(x => !string.IsNullOrEmpty(x.pingming)).ToList())
            {
                m.danhao = danju.dh;              
            } 
            danjumingxitableService.Insertdanjumingxitablelst(danjumingxitables.Where(x=>!string.IsNullOrEmpty(x.pingming )).ToList ());
            //把卷先标识出库单号
            可发卷BLL.卷预出库(juanHaoTables, danju.dh);
           
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据审核(danju.dh);
                }
            }
            else
            {
                单据审核(danju.dh);
            }
        }
        public static void 修改单据(DanjuTable danju, List<danjumingxitable> danjumingxitables, List<JuanHaoTable> juanHaoTables)
        {
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据反审核(danju.dh);
                }
            }
            else
            {
                单据反审核(danju.dh);
            }
            DanjuTableService.UpdateDanjuTable(danju, x => x.dh == danju.dh);
            danjumingxitableService.Deletedanjumingxitable(x => x.danhao == danju.dh);
            foreach (var m in danjumingxitables.Where(x => !string.IsNullOrEmpty(x.pingming)).ToList())
            {
                m.danhao = danju.dh;
            }
            danjumingxitableService.Insertdanjumingxitablelst(danjumingxitables.Where(x => !string.IsNullOrEmpty(x.pingming)).ToList());
            可发卷BLL.卷预出库(juanHaoTables, danju.dh);
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据审核(danju.dh);
                }
            }
        }
        public static void 删除单据(string danhao)
        {
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据反审核(danhao);
                }
            }
            DanjuTableService.DeleteDanjuTable(x => x.dh == danhao);
            danjumingxitableService.Deletedanjumingxitable(x => x.danhao == danhao);
        }
        public static void 单据审核(string danhao)
        {       
            var danju =Connect.dbcontext.Queryable<DanjuTable>().First (x => x.dh == danhao);
            var danjumingxitables = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == danhao);
            danju.zhuangtai = "已审核";
            Connect.dbcontext.Updateable(danju).ExecuteCommand();
            ///把卷全部表示为已出库
            可发卷BLL.卷出库(Connect.dbcontext.Queryable<JuanHaoTable>().Where(x => x.Danhao == danhao).ToList());
            ///
            库存BLL.减少库存 (danjumingxitables, danju);    
        }
        public static void 单据反审核(string danhao)
        {         
            var danju = DanjuTableService.GetOneDanjuTable(x => x.dh == danhao);
            var danjumingxitables = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == danhao);
            danju.zhuangtai = "未审核";
            Connect.dbcontext.Updateable(danju).ExecuteCommand();
            可发卷BLL.卷入库(danhao);
            库存BLL.增加库存 (danjumingxitables, danju);         
        }
    }
}
